#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <map>
#include <list>
#include <string>


#pragma pack(1)
typedef struct
{
	char code[8];
	int dummy;
	int  date[3];
	float data[50];
} FinData; 



FinData * pFinDataSH;
int nFinDataSH = 0;
FinData * pFinDataSZ;
int nFinDataSZ = 0;

char *  prefix_fin = "D:\\tools\\dzh365(64)\\Download\\FIN\\";
char *  fin_file[]={"full_sh.FIN","full_sz.FIN"};
int read_fin(char * file_name,FinData ** pFinData,int * nFinData);

int load_fin_data()
{
	char name[1024];

	strcpy(name, prefix_fin);
	strcat(name, fin_file[0]);
	read_fin(name, &pFinDataSH, &nFinDataSH);

	strcpy(name, prefix_fin);
	strcat(name, fin_file[1]);
	read_fin(name, &pFinDataSZ, &nFinDataSZ);
	return 0;
}


int main(int argc,char *argv[])
{
	load_fin_data();

	for(;;) {
		char code[1024];
		code[0]= 0;
		printf("\n please input stock code, q to quit:\n");
		scanf("%s",code);
		if(code[0] == 'q')
			break;
		FinData * p;
		int ndata = 0;
		if(code[0] == '6'){
			p = pFinDataSH;
			ndata = nFinDataSH;
		}
		else{
			p = pFinDataSZ;
			ndata = nFinDataSZ;
		}
		for(int i=0;i<ndata;p++,i++) {
			if(!strncmp(&(p->code[2]),code,6)){
				printf("\n%d %d %d",p->date[0],p->date[1],p->date[2]);
				for(int j=0;j<51;j++){
					printf("\n%d %f",j, p->data[j]);
				}
			}
		}
	}

	delete pFinDataSH;
	delete pFinDataSZ;
}

int read_fin(char * file_name,FinData ** pFinData,int * nFinData)
{
	FILE *fp;
	if((fp = fopen(file_name,"rb")) == NULL)
	{
		printf("Error: Can't open :%s! ",file_name);
		return -1;
	}

	fseek(fp,0,SEEK_END);

	int filesize = ftell(fp);

	fseek(fp,0,SEEK_SET);
	int ndata = filesize/(54*4);
	*pFinData = new FinData[ndata];
	*nFinData = ndata;

	char head[8];
	fread(&head, sizeof(char)* 8,1, fp);
	int i=0;
	for(FinData * p = *pFinData;i<ndata;p++,i++) {
		if(fread(p, sizeof(FinData), 1, fp ) != 1)
			break;
#ifdef P_DEBUG
			printf("\ncode: %s",p->code);
#endif
	}

	fclose(fp);

#ifdef P_DEBUG
	if(getch()==0) getch();
#endif

	return 0;
}


